<template>
  <div>
    <el-form
      :model="form"
      :rules="rules"
      label-width="150px"
      ref="form"
      class="kylin-form"
    >
      <el-form-item :label="$t('configure.configure_group_name')" prop="name">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item :label="$t('configure.normalLibrary')" prop="standardId">
        <el-select v-model="form.standardId" size="small" class="size">
          <el-option
            v-for="item in normals"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('file')" prop="file">
        <el-upload
          ref="upload"
          :action="action"
          :file-list="fileList"
          :auto-upload="false"
          accept=".xls"
          :on-remove="handleFileChange"
          :on-change="handleFileChange"
        >
          <el-button slot="trigger" size="small" class="kylin-button">{{
            $t("configure.click_select_file")
          }}</el-button>
          <el-button class="kylin-button" @click="downloadTemplate">{{
            $t("excel_template_download")
          }}</el-button>
          <div slot="tip" class="el-upload__tip">
            {{ $t("only_upload_excel") }}
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item :label="$t('configure.ascription')" prop="ascription">
        <el-select v-model="form.ascription" size="small" class="size">
          <el-option
            v-for="item in ascriptions"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('description')" prop="description">
        <el-input v-model="form.description" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">{{ $t("cancel") }}</el-button>
      <el-button class="kylin-button" @click="handleOk">{{
        $t("save")
      }}</el-button>
    </div>
  </div>
</template>

<script>
import { getStandardLib } from "@/api/config";
import { importConfigureGroup } from "@/api/configure_group";
import { downloadTemplate } from "@/api/template";
import { download } from "@/utils";
export default {
  data() {
    let checkFile = (rule, value, callback) => {
      if (this.fileList.length < 1) {
        callback(new Error(this.$t("file_require")));
      } else {
        callback();
      }
    };
    return {
      action: "#",
      fileList: [],
      form: {
        name: "",
        standardId: "",
        ascription: "",
        description: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: this.$t("configure.configure_group_name_require"),
            trigger: "change",
          },
        ],
        standardId: [
          {
            required: true,
            message: this.$t("configure.normal_require"),
            trigger: "change",
          },
        ],
        file: [
          {
            validator: checkFile,
            trigger: "change",
            required: true
          },
        ],
        ascription: [
          {
            required: true,
            message: this.$t("configure.ascription_require"),
            trigger: "change",
          },
        ],
      },
      normals: [],
      ascriptions: [
        { id: "中心", name: "中心" },
        { id: "基地", name: "基地" },
        { id: "部门", name: "部门" },
      ],
    };
  },
  methods: {
    handleCancel() {
      this.$refs.form.resetFields();
      this.$emit("click");
    },
    handleFileChange(file, fileList) {
      if (fileList.length > 1) {
        this.fileList = fileList.slice(-1);
      } else {
          this.fileList = fileList
      }
    },
    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let data = new FormData();
          data.append("name", this.form.name);
          data.append("file", this.fileList[0].raw)
          data.append("standardLibrary", this.form.standardId)
          data.append("groupAttribute", this.form.ascription)
          data.append("description", this.form.description)
          importConfigureGroup(data).then((res) => {
            if (res.data.code === "0") {
              this.$message({
                type: "success",
                message: res.data.message,
              });
              this.$emit("click", "success");
              this.$refs.form.resetFields();
            } else {
              this.$message({
                type: "error",
                message: res.data.message,
              });
            }
          });
        }
      });
    },
    downloadTemplate() {
        downloadTemplate("config_group_template").then((res) => {
        download(res, "config_group_template");
      });
    }
  },
  mounted() {
    getStandardLib({ paged: false }).then((res) => {
      res.data.data.results.map((item) => {
        this.normals.push({
          id: item.id,
          name: `${item.name}-${item.version}-${item.cpuArch}`,
        });
      });
    });
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.size {
  width: 100%;
}
</style>